package com.example.designmodel.chain.dialog

class DialogChain(private val builder: Builder) {
     private var index = 0
     fun proceed(){
      when(index){
          in builder.chainList.indices ->{
              val  dialogIntercept = builder.chainList[index]
              if (dialogIntercept.show()){
                  dialogIntercept.intercept(this)
              }else{
                  builder.chainList[++index].intercept(this)
              }
              index++
          }
          builder.chainList.size->{
              builder.chainList.clear()
              index = 0
          }
      }
     }
    fun proceedNex(){
        ++index
        proceed()
    }
    class Builder(){
        var chainList:MutableList<DialogIntercept> = mutableListOf()
        fun addIntercept(dialogIntercept: DialogIntercept):Builder{
            if (!chainList.contains(dialogIntercept)){
                chainList.add(dialogIntercept)
            }
            return this
        }
        fun build():DialogChain{
            return DialogChain(this)
        }
    }

}